﻿using Iworld.Client.Framework.AuthorService;
using System;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 充值卡记录视图模型
    /// </summary>
    public class RechargeRecordsViewModel : ShowListViewModelBase<RechargeRecordExport, AuthorServiceClient>
    {
        public DateTime? beginTime = null;
        public DateTime? endTime = null;

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                }
            }
        }


        public RechargeRecordsViewModel()
            : base(PageCategory.充值服务.ToString(),Page.充值记录.ToString())
        {
            client.GetRechargeRecordsCompleted += this.WrapEventHandler<GetRechargeRecordsCompletedEventArgs>(GetRechargeRecordsCompleted);
        }

        void GetRechargeRecordsCompleted(object sender, GetRechargeRecordsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }

        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            client.GetRechargeRecordsAsync(new GetRechargeRecordsImport() {
                BeginTime = this.BeginTime,
                EndTime = this.EndTime,
                PageIndex = pageIndex,
                Token = DataManager.GetValue<string>(DataKey.Iworld_Token)
            });
        }

        protected override void Reset(object obj)
        {
            this.BeginTime = null;
            this.EndTime = null;
            this.PageIndex = 1;
            Refresh(null);
        }
    }
}
